import torch

t1 = torch.tensor([5, 4, 3, 2, 1])  # (5)
print(t1.shape)
print(t1)
# 使用torch.diag将一个向量构造为一个 [n, n] 的对角矩阵
# 注意，维度数量一定要是1，即 [n]，不能为 [n, 1] 或 [1, n]
t2 = torch.diag(t1)
print(t2)


t3 = t1.unsqueeze(-1)  # (5, 1)
print(t3.shape)
print(f't3={t3}')
t4 = torch.diag(t3)
print(t4)  # tensor([5])

t5 = t1.unsqueeze(0)  # (1, 5)
print(t5.shape)
print(f't5={t5}')
t6 = torch.diag(t5)
print(t6)  # tensor([5])


m = torch.ones([5]).to(torch.int64) * 2
mul = torch.matmul(t2, m)
print(f't2.shape={t2.shape}')  # (5, 5)
print(f'm.shape={m.shape}') # (5) -> (5, 1)
print(f'mul.shape={mul.shape}')  # (5, 1)
print(f't2={t2}')
print(f'm={m}')
print(f'mul=t2*m={mul}')

print(f't1*m={t1 * m}')


